
import java.util.{Date, TimeZone}

import actors._
import akka.actor.Props
import com.redis.M
import common.JedisTool
import common.Tool._
import play.api.Play.current
import play.api._
import play.api.libs.concurrent.Akka
import play.api.mvc.Results._
import play.api.mvc._
import common.JedisCacheKeys._

import scala.concurrent.Future

/**
 * Created by 林 on 14-4-8.
 */
object Global extends GlobalSettings {
  override  def onStart(app:Application)={
    Logger.info(TimeZone.getDefault.toString)
    val timezone = TimeZone.getTimeZone("GMT+8")
    TimeZone.setDefault(timezone)
    Logger.info(new Date().sdatetime)
    super.onStart(app)
    //启动消息发送Actor
    Akka.system.actorOf(Props[DbTaskServer], name = "db_task_server_actor")
    val taskServer=Akka.system.actorOf(Props[TaskServer], name = "task_server_actor")
    val cacheServer=Akka.system.actorOf(Props[CacheTaskServer], name = "cache_server_actor")
    //发送消息
    cacheServer ! SETTING_MESSAGE_KEY
    cacheServer ! INIT_CACHE_KEY
    cacheServer ! WEIXIN_ID_CACHE_KEY
    taskServer ! new LoopTaskMessage
      //追踪缓存变动
      JedisTool.getJedis.pSubscribe("CACHE*") { msg =>
        msg match {
          case M(channel, message) =>
            Logger.info("update cache "+channel + ":"+message)
            cacheServer ! CacheValue(channel,message)
          case _ =>
        }
      }
  }
  override def onError(request: RequestHeader, ex: Throwable) = {
    sendStackTrace(ex,request.toJson)
    Logger.info("error:"+request.toJson,ex)
    Future.successful(InternalServerError(ex.getMessage
    ))
  }
  override def onHandlerNotFound(request: RequestHeader) = {
    Future.successful(NotFound(
      request.path
    ))
  }
  //发送邮件给开发组
  def sendStackTrace(e:Throwable,msg:String)={
    Logger.info("find send out error mail",e)
  }

}
